package com.frog.service.friend.model;

import com.frog.service.friend.enums.FriendListTypeEnum;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * 分页请求入参
 */
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class PageFriendReqModel {
    private Integer userId;
    private Integer friendUserId;
    private FriendListTypeEnum type;
    private int page;
    private int pageSize;
    private boolean hasMore;

    public boolean verify() {
        return userId == null || type == null;
    }

    public void initPage() {
        this.page = page > 0 ? page : 1;
        this.pageSize = pageSize < 0 ? -1 : pageSize == 0 ? 10 : pageSize;
    }

    public PageModel calculateStartAndEndRow() {
        initPage();
        long startRow = pageSize > 0
                ? this.page > 0 ? (long) (this.page - 1) * this.pageSize : 0
                : 0;

        long endedRow = pageSize > 0
                ? startRow + this.pageSize * (this.page > 0 ? 1 : 0) - 1
                : -1;
        return new PageModel(startRow, endedRow);
    }

    @Data
    @Builder
    @AllArgsConstructor
    @NoArgsConstructor
    public static class PageModel implements Serializable {
        // 起始行
        private long startRow;
        // 末行
        private long endRow;
    }

}
